/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jeesite.common.cache.CacheUtils;
import com.jeesite.common.lang.ObjectUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.service.ServiceException;
import com.jeesite.modules.aliyun.msg.AliyunSmsUtils;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.kj.entity.KjIntegralAuth;
import com.jeesite.modules.kj.dao.KjIntegralAuthDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * kj_integral_authService
 * @author hzy
 * @version 2021-07-13
 */
@Service
@Transactional(readOnly=true)
public class KjIntegralAuthService extends CrudService<KjIntegralAuthDao, KjIntegralAuth> {

	@Value("${msg.sms.template.authAccess}")
	private String authAccessTpl;
	@Value("${msg.sms.template.authFailed}")
	private String authFailedTpl;
	/**
	 * 获取单条数据
	 * @param kjIntegralAuth
	 * @return
	 */
	@Override
	public KjIntegralAuth get(KjIntegralAuth kjIntegralAuth) {
		return super.get(kjIntegralAuth);
	}

	/**
	 * 查询分页数据
	 * @param kjIntegralAuth 查询条件
	 * @param kjIntegralAuth.page 分页对象
	 * @return
	 */
	@Override
	public Page<KjIntegralAuth> findPage(KjIntegralAuth kjIntegralAuth) {
		return super.findPage(kjIntegralAuth);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param kjIntegralAuth
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(KjIntegralAuth kjIntegralAuth) {
		super.save(kjIntegralAuth);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(kjIntegralAuth.getId(), "kjIntegralAuth_image");
	}

	/**
	 * 更新状态
	 * @param kjIntegralAuth
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(KjIntegralAuth kjIntegralAuth) {
		super.updateStatus(kjIntegralAuth);
	}

	/**
	 * 删除数据
	 * @param kjIntegralAuth
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(KjIntegralAuth kjIntegralAuth) {
		super.delete(kjIntegralAuth);
	}

	public void auth(KjIntegralAuth auth){
		KjMember member = null;
		try {
			member = KjMemberUtil.getCurrentMember(auth.getKjMember().getId());
		} catch (FreezMemberException e) {
			e.printStackTrace();
		}
		if(auth.getApproveStatus().equals(KjIntegralAuth.OK)){
			authSms(member.getMobile(), authAccessTpl);
		}else if(auth.getApproveStatus().equals(KjIntegralAuth.REJECT)){
			authSms(member.getMobile(), authFailedTpl);
		}
	}
	public void authSms(String mobile, String tpl){
		Map<String, Object> tplData = new HashMap<>();
		String result = AliyunSmsUtils.sendByTemplate(mobile, tpl, JSON.toJSONString(tplData));
		Map<String, Object> map = JsonMapper.fromJson(result, Map.class);
		// 发送成功
		if (ObjectUtils.toInteger(map.get("result")) == 0){

		}//			 发送失败
		else{
			logger.error(text("短信发送失败{0}", result));
		}
	}
}
